<?php 
class Description { 
	private $mySqlConnector;
	public function __construct($_mySqlConnector) {
		$this->mySqlConnector = $_mySqlConnector;
	}
	
	public function printJS($key, $varName) { 
		echo "\nvar {$varName} = \"".$this->get($key)."\";\n";
	}
	
	public function update($key, $newDesc) { 
		$newDesc = $this->getCleareString($newDesc);
		$query = "empty script";
		if($this->get("asd")!=''){
			$query = "UPDATE `description` 
				SET 
					`desc`='{$newDesc}'
				WHERE desc_name=\"{$key}\"";
		} else {
			$query = "INSERT INTO `description`(`desc_name`, `desc`) VALUES ('{$key}','{$newDesc}')";
		}
		$resultQuerry = mysql_query($query) or die (mysql_error());
	}
	
	public function get($key) { 
		$isConnected = $this->mySqlConnector->isConnected();
		$this->mySqlConnector->connect();
		$query = "select * FROM `description` WHERE desc_name=\"{$key}\"";
		$resultQuerry = mysql_query($query) or die (mysql_error());
		$index = 0;
		if(!$isConnected){
			$this->mySqlConnector->disconnect();
		}
		$res = mysql_fetch_assoc($resultQuerry)['desc'];
		return $this->getCleareString($res);
	} 
	
	private function getCleareString($str) { 
		$str = str_replace("\n\r", "</br>", $str);
		$str = str_replace("\r\n", "</br>", $str);
		$str = str_replace("\n", "</br>", $str);
		$str = str_replace("\r", "</br>", $str);
		return $str;
	}
} 